package com.aceitunaproject.gameclient;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Point;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.aceitunaproject.shared.gamelement.Installation;
import com.aceitunaproject.shared.gamelement.Settlement;
import com.aceitunaproject.shared.gamelement.Unit;
import com.aceitunaproject.shared.map.MapTile;

public class AceitunaView {

	private JFrame mainFrame;
	private List<MapTile> mapModel;
	private MapArea mapArea;
	private GameClient controller;
	private DetailsPanel detailsPanel;
	private ElementDetails elementDetails;
	private JPanel sidePanel;
	private OrdersPanel ordersPanel;

	private boolean secondUnitSelection = false;

	public AceitunaView(List<MapTile> mapModel, GameClient controller) {
		this.mapModel = mapModel;
		this.controller = controller;

		mapArea = new MapArea(mapModel, this);
		detailsPanel = new DetailsPanel(this);
		elementDetails = new ElementDetails();
		ordersPanel = new OrdersPanel(controller);
		mainFrame = new JFrame("Aceituna Game Client");
		sidePanel = new JPanel();

		Container container = mainFrame.getContentPane();
		container.setLayout(new BorderLayout());
		container.add(mapArea, BorderLayout.CENTER);
		container.add(sidePanel, BorderLayout.EAST);
		container.add(ordersPanel, BorderLayout.SOUTH);
		sidePanel.setLayout(new BorderLayout());
		sidePanel.add(detailsPanel, BorderLayout.NORTH);
		sidePanel.add(elementDetails, BorderLayout.SOUTH);
		sidePanel.setBorder(BorderFactory.createEtchedBorder());
		mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.setVisible(true);
	}

	public void displayMessage(String message) {
		System.out.println(message);
		JOptionPane.showMessageDialog(mainFrame, message);
	}

	public void refresh() {
		mapArea.setMapModel(mapModel);
		mapArea.repaint();
	}

	public void setModel(List<MapTile> mapModel) {
		this.mapModel = mapModel;

	}

	public void mapClicked(EnumMouseButton button, Point point) {
		controller.tileClicked(button, getTile(point.x, point.y));
		elementDetails.removeAll();
	}

	private MapTile getTile(int x, int y) {
		for (MapTile tile : mapModel) {
			if (tile.getPosx() == x && tile.getPosy() == y) {
				return tile;
			}
		}
		return null;
	}

	public void selectTile(MapTile mapTile) {
		detailsPanel.setTile(mapTile);

	}

	public void settlementSelected(Settlement tileSettlement) {
		if (!secondUnitSelection) {
		elementDetails.setSettlement(tileSettlement);
		ordersPanel.setForSettlement(tileSettlement);
		sidePanel.revalidate();
		controller.setSelectedSettlement(tileSettlement);
		}

	}

	public void installationSelected(Installation installation) {
		if (!secondUnitSelection) {
		elementDetails.setInstallation(installation);
		ordersPanel.setForInstallation(installation);
		sidePanel.revalidate();
		controller.setSelectedInstallation(installation);
		}
	}

	public void unitSelected(Unit unit) {
		if (!secondUnitSelection) {
			ordersPanel.setForUnit(unit);
			elementDetails.setUnit(unit);
			sidePanel.revalidate();
			controller.setSelectedUnit(unit);
		} else {
			controller.secondUnitSelected(unit);
		}
	}

	public void nothingSelected() {
		ordersPanel.setForNothing();

	}

	public void SecondUnitSelection(boolean secondUnitSelection) {

		this.secondUnitSelection = secondUnitSelection;
		if (secondUnitSelection == true) {
			ordersPanel.setForCancelOnly();
		} else {
			ordersPanel.setForNothing();
		}
	}

}
